package o.mrs;


import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddPatient extends Activity{
	
	private EditText fnameText;
	private EditText lnameText;
	private EditText ageText;
	private EditText genderText;
	private EditText id;
	private Button boton;
	
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.addpatient);
	       
	        fnameText=(EditText) findViewById(R.id.addfname);
	        lnameText=(EditText) findViewById(R.id.addlname);
	        ageText=(EditText) findViewById(R.id.addage);
	        genderText=(EditText) findViewById(R.id.addGenere);
	        id=(EditText) findViewById(R.id.addid);
	        boton=(Button) findViewById(R.id.addsave); 
	        
	      
	        boton.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					
					try {
						StringEntity inputAddPerson = new StringEntity(
								"{\"names\":[{\"givenName\": \""+fnameText.getText().toString()+"\",\"familyName\":\""+lnameText.getText().toString()+"\"}],\"gender\":\""+genderText.getText().toString()+"\",\"age\":"+ageText.getText().toString()+"}");
					 	
						inputAddPerson.setContentType("application/json");
						ApiRest.getRequestPost("person", inputAddPerson);
												
						//Obtiene uuid de la persona agregada						 
						JSONObject jsonObject = new JSONObject(ApiRest.getRequestGet("person?q="+fnameText.getText().toString()+" "+lnameText.getText().toString()));						 
				     	JSONArray arrayResult = (JSONArray) jsonObject.get("results");    				     				      
				     		JSONObject registro = (JSONObject) arrayResult.get(0);
				     		String uuid = (String) registro.get("uuid"); 	     				 	     						     		 			     						        	
				     
				     		///////////////obitne identificador /////////
				     		String output = ApiRest.getRequestGet("patientidentifiertype");
				     		JSONObject jsonObje = new JSONObject(output);
				     		JSONArray arrayIdType = (JSONArray) jsonObje.get("results");
				     		JSONObject unk = (JSONObject) arrayIdType.get(0);
				     		
				     		String unknown = (String) unk.get("uuid");
				     		
				     		///////////////obtiene location///////
				     		output = ApiRest.getRequestGet("location");
				     		jsonObje = new JSONObject(output);
				     		arrayIdType = (JSONArray) jsonObje.get("results");
				     		unk = (JSONObject) arrayIdType.get(0);
				     		String locat = (String) unk.get("uuid");
				     		
				     		
				     		StringEntity inputAddPatient = new StringEntity(
									"{\"person\": \"" + uuid
											+ "\", \"identifiers\": [{\"identifier\""
											+ id.getText().toString() + "\", \"identifierType\""
											+ unknown + "\", \"location\""
											+ locat + "\", \"preferred\":true}]}");
							
				     		
				     		inputAddPatient.setContentType("application/json");
						ApiRest.getRequestPost("patient", inputAddPatient);
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					Intent i = new Intent();
					i.setClass(getApplicationContext(),MainMenu.class);	
					startActivity(i); 
					
					
				}
			});
				
					
					
					
    
	    }
}
